package com.sskj.fabi.ui.activity;

import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.binaryfork.spanny.Spanny;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.bean.FabiOrderStatusEnum;
import com.sskj.fabi.bean.OrderDetailBean;
import com.sskj.fabi.presenter.OrderRecordDetailActivityPresenter;
import com.sskj.fabi.util.TipFabiUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.enums.FabiBuySellEnum;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.TimeFormatUtil;
import com.sskj.lib.util.TipUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.FABI_ORDER_RECORD_DETAIL)
public class OrderRecordDetailActivity extends BaseActivity<OrderRecordDetailActivityPresenter> {
    @BindView(R2.id.tvTitleOrder)
    TextView tvTitleOrder;
    @BindView(R2.id.tvOrderNum)
    TextView tvOrderNum;
    @BindView(R2.id.tvStatus)
    TextView tvStatus;
    @BindView(R2.id.tvTradeType)
    TextView tvTradeType;
    @BindView(R2.id.tvPrice)
    TextView tvPrice;
    @BindView(R2.id.tvNum)
    TextView tvNum;
    @BindView(R2.id.tvAllMoney)
    TextView tvAllMoney;
    @BindView(R2.id.tvRemark)
    TextView tvRemark;
    @BindView(R2.id.pay_type)
    TextView payType;
    @BindView(R2.id.tvAlipayAccount)
    TextView tvAlipayAccount;
    @BindView(R2.id.ivAlipay)
    ImageView ivAlipay;
    @BindView(R2.id.llAlipay)
    LinearLayout llAlipay;
    @BindView(R2.id.tvWeChatAccount)
    TextView tvWeChatAccount;
    @BindView(R2.id.ivWechat)
    ImageView ivWechat;
    @BindView(R2.id.llWeChat)
    LinearLayout llWeChat;
    @BindView(R2.id.tvBankAccount)
    TextView tvBankAccount;
    @BindView(R2.id.tvBank)
    TextView tvBank;
    @BindView(R2.id.llBank)
    LinearLayout llBank;
    @BindView(R2.id.tvBuyerFirst)
    TextView tvBuyerFirst;
    @BindView(R2.id.tvBuyerSecond)
    TextView tvBuyerSecond;
    @BindView(R2.id.btBuyerSure)
    Button btBuyerSure;
    @BindView(R2.id.tvBuyerCancel)
    TextView tvBuyerCancel;
    @BindView(R2.id.llBuyerWaitPay)
    LinearLayout llBuyerWaitPay;
    @BindView(R2.id.tvSellerWaitCoinFirst)
    TextView tvSellerWaitCoinFirst;
    @BindView(R2.id.tvSellerWaitCoinSecond)
    TextView tvSellerWaitCoinSecond;
    @BindView(R2.id.btSellerWaitCoinSure)
    Button btSellerWaitCoinSure;
    @BindView(R2.id.tvSellerWaitCoinCancel)
    TextView tvSellerWaitCoinCancel;
    @BindView(R2.id.llSellerWaitCoin)
    LinearLayout llSellerWaitCoin;
    @BindView(R2.id.tvSellerWaitPayFirst)
    TextView tvSellerWaitPayFirst;
    @BindView(R2.id.llSellerWaitPay)
    LinearLayout llSellerWaitPay;
    @BindView(R2.id.tvBuyerWaitCoinFirst)
    TextView tvBuyerWaitCoinFirst;
    @BindView(R2.id.tvBuyerWaitCoinSecond)
    TextView tvBuyerWaitCoinSecond;

    @BindView(R2.id.llBuyerWaitCoin)
    LinearLayout llBuyerWaitCoin;
    @BindView(R2.id.tvAppealingFirst)
    TextView tvAppealingFirst;
    @BindView(R2.id.tvAppealingSecond)
    TextView tvAppealingSecond;
    @BindView(R2.id.viewAppealTip)
    View viewAppealTip;
    @BindView(R2.id.llAppealing)
    LinearLayout llAppealing;
    @BindView(R2.id.tvCancel)
    TextView tvCancel;
    @BindView(R2.id.llCancel)
    LinearLayout llCancel;
    @BindView(R2.id.tvComplete)
    TextView tvComplete;
    @BindView(R2.id.tv_phone)
    TextView tv_phone;
    @BindView(R2.id.llComplete)
    LinearLayout llComplete;


    @BindView(R2.id.checkBoxAlipay)
    CheckBox checkBoxAlipay;
    @BindView(R2.id.checkBoxBank)
    CheckBox checkBoxBank;
    @BindView(R2.id.checkBoxWechat)
    CheckBox checkBoxWechat;
    @BindView(R2.id.tvPhone)
    TextView tvPhone;
    @BindView(R2.id.ivPhoneCopy)
    ImageView ivPhoneCopy;
    @BindView(R2.id.tv_name)
    TextView tv_name;
    @BindView(R2.id.tvBuySellInfoTitle)
    TextView tvBuySellInfoTitle;
    @BindView(R2.id.include_pay_type_select)
    LinearLayout includePayTypeSelect;
    @BindView(R2.id.include_pay_type)
    LinearLayout includePayType;
    @BindView(R2.id.llWeChatSelect)
    LinearLayout llWeChatSelect;
    @BindView(R2.id.llAlipaySelect)
    LinearLayout llAlipaySelect;
    @BindView(R2.id.llBankSelect)
    LinearLayout llBankSelect;
    @BindView(R2.id.tvAlipayAccountSelect)
    TextView tvAlipayAccountSelect;
    @BindView(R2.id.ivAlipaySelect)
    ImageView ivAlipaySelect;
    @BindView(R2.id.tvWeChatAccountSelect)
    TextView tvWeChatAccountSelect;
    @BindView(R2.id.ivWechatSelect)
    ImageView ivWechatSelect;
    @BindView(R2.id.ivBankSelectCopy)
    ImageView ivCopyBankAccount;
    @BindView(R2.id.tv_buy_bank_name)
    TextView tvBuyBankName;
    @BindView(R2.id.tv_sale_bank_name)
    TextView tvSaleBankName;
    @BindView(R2.id.tv_buy_bank_branch_name)
    TextView tvBuyBankBranchName;
    @BindView(R2.id.tv_sale_bank_branch_name)
    TextView tvSaleBankBranchName;
    @BindView(R2.id.tvBankAccountSelect)
    TextView tvBankAccountSelect;
    @BindView(R2.id.tv_price_des)
    TextView tvPriceDes;

    @BindView(R2.id.iv_copy)
    ImageView ivCopy;

    @BindView(R2.id.ivAlipaySelectCopy)
    ImageView ivAlipaySelectCopy;
    @BindView(R2.id.ivCopySaleBankAccount)
    ImageView ivCopySaleBankAccount;

    @BindView(R2.id.ivWechatSelectCopy)
    ImageView ivWechatSelectCopy;
    @BindView(R2.id.ivWechatSaleSelectCopy)
    ImageView ivWechatSaleSelectCopy;
    @BindView(R2.id.ivAlipaySaleSelectCopy)
    ImageView ivAlipaySaleSelectCopy;

    private HashMap<String, String> payTipMap;
    private OrderDetailBean data;
    private Disposable timerDispo;
    private MaterialDialog payTipDialog;
    private MaterialDialog tipCancleDialog;
    private Disposable detailDispo;
    private MaterialDialog codeDialog;

    private int positionPay;
    private boolean isChoose = true; // 用户是否点击选择支付方式
    private String orderId;
    @Autowired
    boolean isBuyer;
    @Autowired
    String orderNum;
    private MaterialDialog materialDialogAppeal;
    private boolean isSelect;

    @Override
    protected int getLayoutId() {
        return R.layout.fabi_activity_order_record_detail;
    }

    @Override
    public OrderRecordDetailActivityPresenter getPresenter() {
        return new OrderRecordDetailActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(getString(R.string.fabiorderRecordDetailActivity1));
        ARouter.getInstance().inject(this);
        mPresenter.setOrderNum(orderNum);

        llAlipay.setVisibility(View.GONE);
        llBank.setVisibility(View.GONE);
        llWeChat.setVisibility(View.GONE);

    }

    @Override
    protected void initData() {
        payTipMap = new HashMap<>();
        payTipMap.put("1", App.INSTANCE.getString(R.string.fabi_orderRecordDetailActivity2));
        payTipMap.put("2", App.INSTANCE.getString(R.string.fabi_orderRecordDetailActivity3));
        mPresenter.getOrderInfo(orderNum);
        ClickUtil.click(ivCopy, () -> {
            CopyUtil.copy(isBuyer ? data.getSellPhone() : data.getBuyPhone());
        });
    }

    public void updateUI(OrderDetailBean data) {
        this.data = data;
        orderId = data.getId();
        llAlipay.setVisibility(View.GONE);
        llBank.setVisibility(View.GONE);
        llWeChat.setVisibility(View.GONE);
        DisposUtil.close(timerDispo);
        Map<String, String> buyerMap = new HashMap<>();
        Map<String, String> sellerMap = new HashMap<>();
        buyerMap.put("1", FabiOrderStatusEnum.待付款.getTitle());
        buyerMap.put("2", FabiOrderStatusEnum.等待对方放币.getTitle());
        buyerMap.put("3", FabiOrderStatusEnum.已完成.getTitle());
        buyerMap.put("4", FabiOrderStatusEnum.申诉中.getTitle());
        buyerMap.put("5", FabiOrderStatusEnum.已取消.getTitle());
        sellerMap.put("1", FabiOrderStatusEnum.等待对方付款.getTitle());
        sellerMap.put("2", FabiOrderStatusEnum.待放币.getTitle());
        sellerMap.put("3", FabiOrderStatusEnum.已完成.getTitle());
        sellerMap.put("4", FabiOrderStatusEnum.申诉中.getTitle());
        sellerMap.put("5", FabiOrderStatusEnum.已取消.getTitle());

        if (isBuyer && data.getStatus() == 1) {
            includePayTypeSelect.setVisibility(View.VISIBLE);
            includePayType.setVisibility(View.GONE);
            llWeChatSelect.setVisibility(View.GONE);
            llAlipaySelect.setVisibility(View.GONE);
            llBankSelect.setVisibility(View.GONE);
//            isSelect = false;
            Flowable.fromIterable(data.getStockUserPayWays())
                    .subscribe(stockUserPayWaysBean -> {
                        switch (stockUserPayWaysBean.getPayType()) {
                            case "1"://微信
//                                if (!isSelect) {
//                                    isSelect = true;
//                                    checkBoxWechat.setChecked(true);
//                                }
                                checkBoxWechat.setVisibility(View.VISIBLE);
                                llWeChatSelect.setVisibility(View.VISIBLE);
                                tvWeChatAccountSelect.setText(stockUserPayWaysBean.getWxAccount());
                                ClickUtil.click(llWeChatSelect, () -> {
                                    if (!checkBoxWechat.isChecked()) {
                                        checkBoxWechat.setChecked(true);
                                        checkBoxAlipay.setChecked(false);
                                        checkBoxBank.setChecked(false);
                                    }
                                });
                                ClickUtil.click(ivWechatSelect, () -> {
                                    showQR(stockUserPayWaysBean.getWxImg());
                                });
                                ClickUtil.click(ivWechatSelectCopy, () -> {
                                    CopyUtil.copy(stockUserPayWaysBean.getWxAccount());
                                });
                                break;
                            case "2"://支付宝
//                                if (!isSelect) {
//                                    isSelect = true;
//                                    checkBoxAlipay.setChecked(true);
//                                }
                                checkBoxAlipay.setVisibility(View.VISIBLE);
                                llAlipaySelect.setVisibility(View.VISIBLE);
                                tvAlipayAccountSelect.setText(stockUserPayWaysBean.getAlipayAccount());
                                ClickUtil.click(llAlipaySelect, () -> {
                                    if (!checkBoxAlipay.isChecked()) {
                                        checkBoxAlipay.setChecked(true);
                                        checkBoxWechat.setChecked(false);
                                        checkBoxBank.setChecked(false);
                                    }
                                });
                                ClickUtil.click(ivAlipaySelect, () -> {
                                    showQR(stockUserPayWaysBean.getAliImg());
                                });
                                ClickUtil.click(ivAlipaySelectCopy, () -> {
                                    CopyUtil.copy(stockUserPayWaysBean.getAlipayAccount());
                                });
                                break;
                            case "3"://银行卡
//                                if (!isSelect) {
//                                    isSelect = true;
//                                    checkBoxBank.setChecked(true);
//                                }
                                checkBoxBank.setVisibility(View.VISIBLE);
                                llBankSelect.setVisibility(View.VISIBLE);
                                tvBuyBankName.setText(stockUserPayWaysBean.getBankCardOpenBank());
                                tvBankAccountSelect.setText(data.getSellUsername() + stockUserPayWaysBean.getBankCardNo());
                                tvBuyBankBranchName.setText(stockUserPayWaysBean.getBankCardUnionNo());
                                ClickUtil.click(llBankSelect, () -> {
                                    if (!checkBoxBank.isChecked()) {
                                        checkBoxBank.setChecked(true);
                                        checkBoxWechat.setChecked(false);
                                        checkBoxAlipay.setChecked(false);
                                    }
                                });
                                ClickUtil.click(ivCopyBankAccount, () -> {
                                    CopyUtil.copy(stockUserPayWaysBean.getBankCardNo());

                                });
                                break;

                        }
                    });
        } else {
            includePayTypeSelect.setVisibility(View.GONE);
            includePayType.setVisibility(View.VISIBLE);
            llWeChat.setVisibility(View.GONE);
            llAlipay.setVisibility(View.GONE);
            llBank.setVisibility(View.GONE);
            Flowable<OrderDetailBean.StockUserPayWaysBean> stockUserPayWaysBeanFlowable = Flowable.fromIterable(data.getStockUserPayWays());
            if (!(!isBuyer && data.getStatus() == 1) && data.getStatus() != 5) {
                stockUserPayWaysBeanFlowable = stockUserPayWaysBeanFlowable
                        .filter(stockUserPayWaysBean -> data.getPayType().contains(stockUserPayWaysBean.getPayType()));
            }
            stockUserPayWaysBeanFlowable.subscribe(stockUserPayWaysBean -> {
                switch (stockUserPayWaysBean.getPayType()) {
                    case "1"://微信
                        llWeChat.setVisibility(View.VISIBLE);
                        checkBoxWechat.setVisibility(View.GONE);
                        tvWeChatAccount.setText(stockUserPayWaysBean.getWxAccount());
                        ClickUtil.click(ivWechat, () -> {
                            showQR(stockUserPayWaysBean.getWxImg());
                        });
                        ClickUtil.click(ivWechatSaleSelectCopy, () -> {
                            CopyUtil.copy(stockUserPayWaysBean.getWxAccount());
                        });
                        break;
                    case "2"://支付宝
                        llAlipay.setVisibility(View.VISIBLE);
                        checkBoxAlipay.setVisibility(View.GONE);
                        tvAlipayAccount.setText(stockUserPayWaysBean.getAlipayAccount());
                        ClickUtil.click(ivAlipay, () -> {
                            showQR(stockUserPayWaysBean.getAliImg());
                        });
                        ClickUtil.click(ivAlipaySaleSelectCopy, () -> {
                            CopyUtil.copy(stockUserPayWaysBean.getAlipayAccount());
                        });
                        break;
                    case "3"://银行卡
                        llBank.setVisibility(View.VISIBLE);
                        checkBoxBank.setVisibility(View.GONE);
                        tvBankAccount.setText(data.getSellUsername() + stockUserPayWaysBean.getBankCardNo());
                        tvSaleBankName.setText(stockUserPayWaysBean.getBankCardOpenBank());
//                                tvBank.setText(stockUserPayWaysBean.getBankCardOpenBank());
                        tvSaleBankBranchName.setText(stockUserPayWaysBean.getBankCardUnionNo());
                        ClickUtil.click(ivCopySaleBankAccount, () -> {
                            CopyUtil.copy(stockUserPayWaysBean.getBankCardNo());
                        });
                        break;
                }
            });
        }

        Spanny spanny = new Spanny(String.format(getString(R.string.fabi_orderRecordDetailActivity4), isBuyer ? data.getSellUsername() : data.getBuyUsername()))
                .append(isBuyer ? FabiBuySellEnum.BUY.getTitle() : FabiBuySellEnum.SELL.getTitle(), new ForegroundColorSpan(color(R.color.fabiApp)))
                .append(String.format(" %s USDT", CoinUtil.keepUSDT(data.getTotalNum())));

        tvTitleOrder.setText(spanny);
//        tvBuySellInfoTitle.setText(isBuyer ? getString(R.string.fabi_orderRecordDetailActivity5) : getString(R.string.fabi_orderRecordDetailActivity6));
        tvPriceDes.setText(isBuyer ? getString(R.string.fabi_orderRecordDetailActivity7) : getString(R.string.fabi_orderRecordDetailActivity8));
        tvOrderNum.setText(data.getDealEntrustNo());
        tvStatus.setText(isBuyer ? buyerMap.get(String.valueOf(data.getStatus())) : sellerMap.get(String.valueOf(data.getStatus())));
        tvTradeType.setText(String.format(getString(R.string.fabi_orderRecordDetailActivity9), isBuyer ? FabiBuySellEnum.BUY.getTitle() : FabiBuySellEnum.SELL.getTitle()));
        tvPrice.setText(String.format("%s CNY", CoinUtil.keepRMB(data.getPrice())));
        tvNum.setText(String.format("%s USDT", CoinUtil.keepUSDT(data.getTotalNum())));
        tvAllMoney.setText(String.format("%s CNY", CoinUtil.keepRMB(data.getTotalPrice())));
        tvRemark.setText(TextUtils.isEmpty(data.getRemarkInfo()) ? "--" : data.getRemarkInfo());
        tv_phone.setText(isBuyer ? data.getSellPhone() : data.getBuyPhone());
        tv_name.setText(isBuyer ? data.getSellUsername() : data.getBuyUsername());


        llSellerWaitPay.setVisibility(View.GONE);
        llBuyerWaitPay.setVisibility(View.GONE);
        llBuyerWaitCoin.setVisibility(View.GONE);
        llSellerWaitCoin.setVisibility(View.GONE);
        llAppealing.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llComplete.setVisibility(View.GONE);
        DisposUtil.close(timerDispo);

        if (isBuyer) {//买家页面
            switch (data.getStatus()) {
                case 1://未支付
                    buyerUnPay();
                    break;
                case 2://等待放币
                    buyerWaitCoin();
                    break;
                case 4://卖家申诉中
                    buyerAndSellerAppeal();
                    break;
                case 5://订单取消
                    orderCancel();
                    break;
                case 3://订单完成
                    orderComplete();
                    break;
            }
        } else {//卖家页面
            switch (data.getStatus()) {
                case 1://待付款
                    sellerUnPay();
                    break;
                case 2://待放币
                    sellerWaitCoin();
                    break;
                case 4://卖家申诉中
                    buyerAndSellerAppeal();
                    break;
                case 5://订单取消
                    orderCancel();
                    break;
                case 3://订单完成
                    orderComplete();
                    break;

            }
        }
    }

    /**
     * 卖家等待放币
     */
    private void sellerWaitCoin() {
        llSellerWaitCoin.setVisibility(View.VISIBLE);
        tvSellerWaitCoinFirst.setText(String.format(getString(R.string.fabi_orderRecordDetailActivity10), data.getRefer()));

        String time = TimeFormatUtil.parseTime(data.getCountDownTime());
        tvSellerWaitCoinSecond.setText(String.format(getString(R.string.fabi_orderRecordDetailActivity11), time));


        timerDispo = Flowable.intervalRange(1, data.getCountDownTime(), 0, 1, TimeUnit.SECONDS)
                .map(i -> data.getCountDownTime() - i)
                .compose(RxSchedulersHelper.transformer())
                .subscribe(i -> {
                    String timeIn = TimeFormatUtil.parseTime(i);
                    tvSellerWaitCoinSecond.setText(String.format(getString(R.string.fabi_orderRecordDetailActivity11), timeIn));

                });
        ClickUtil.click(tvSellerWaitCoinCancel, () -> {//申诉
            showAppeal();
        });
        ClickUtil.click(btSellerWaitCoinSure, () -> {//放币
            showPwdBottom();
        });
        refreshTimerOrder();
    }

    /**
     * 显示申诉弹窗
     */
    private void showAppeal() {
        materialDialogAppeal = TipFabiUtil.showAppealTip(this, str -> {
            materialDialogAppeal.dismiss();
            mPresenter.allegeOrder(orderId, str[0]);

        });
        materialDialogAppeal.show();


    }

    /**
     * 放币资金密码弹窗
     */
    private void showPwdBottom() {

        if (codeDialog == null) {
            codeDialog = TipFabiUtil.sendCoin(this, str -> {
                codeDialog.dismiss();
                mPresenter.letGo(orderId, str[0]);
            });
        }
        codeDialog.show();
    }

    /**
     * 卖家未支付
     */
    private void sellerUnPay() {
        llSellerWaitPay.setVisibility(View.VISIBLE);
        String time = TimeFormatUtil.parseTime(data.getCountDownTime());
        Spanny spanny = new Spanny(String.format(getString(R.string.fabiorderRecordDetailActivity14), data.getBuyUsername()))
                .append(time, new ForegroundColorSpan(color(R.color.fabiApp)))
                .append(getString(R.string.fabi_orderRecordDetailActivity13))
                .append(String.format(getString(R.string.fabi_orderRecordDetailActivity14), data.getRefer()), new ForegroundColorSpan(color(R.color.fabiApp)));
        tvSellerWaitPayFirst.setText(spanny);

        timerDispo = Flowable.intervalRange(1, data.getCountDownTime(), 0, 1, TimeUnit.SECONDS)
                .map(i -> data.getCountDownTime() - i)
                .compose(RxSchedulersHelper.transformer())
                .subscribe(i -> {
                    String timeIn = TimeFormatUtil.parseTime(i);
                    Spanny spannyIn = new Spanny(String.format(getString(R.string.fabiorderRecordDetailActivity14), data.getBuyUsername()))
                            .append(timeIn, new ForegroundColorSpan(color(R.color.fabiApp)))
                            .append(getString(R.string.fabi_orderRecordDetailActivity13))
                            .append(String.format(getString(R.string.fabi_orderRecordDetailActivity14), data.getRefer()), new ForegroundColorSpan(color(R.color.fabiApp)));
                    tvSellerWaitPayFirst.setText(spannyIn);
                });
//        ClickUtil.click(btSellerWaitPaySure, () -> {
//            showCancelTip();
//        });
        refreshTimerOrder();
    }


    /**
     * 订单完成
     */
    private void orderComplete() {
        tvComplete.setText(String.format(getString(R.string.fabi_orderRecordDetailActivity15), data.getRefer()));
        llComplete.setVisibility(View.VISIBLE);
        DisposUtil.close(detailDispo);
    }

    /**
     * 订单取消
     */
    private void orderCancel() {
        llCancel.setVisibility(View.VISIBLE);
        DisposUtil.close(detailDispo);

    }

    private void buyerAndSellerAppeal() {
        llAppealing.setVisibility(View.VISIBLE);
        if (isBuyer) {
            viewAppealTip.setVisibility(View.GONE);
            Spanny append = new Spanny(getString(R.string.fabi_orderRecordDetailActivity16))
                    .append(getString(R.string.fabi_orderRecordDetailActivity17) + data.getRefer(), new ForegroundColorSpan(color(R.color.fabiApp)));
            tvAppealingFirst.setText(append);
            tvAppealingSecond.setText(getString(R.string.fabi_orderRecordDetailActivity18) +
                    getString(R.string.fabi_orderRecordDetailActivity19));
        } else {
            viewAppealTip.setVisibility(View.VISIBLE);

            Spanny append = new Spanny(getString(R.string.fabi_orderRecordDetailActivity16))
                    .append(getString(R.string.fabi_orderRecordDetailActivity20) + data.getCommand(), new ForegroundColorSpan(color(R.color.fabiApp)));
            tvAppealingFirst.setText(append);
            tvAppealingSecond.setText(getString(R.string.fabi_orderRecordDetailActivity21));
        }
    }

    /**
     * 买家未支付
     */
    private void buyerUnPay() {
        llBuyerWaitPay.setVisibility(View.VISIBLE);

        long countDown = data.getCountDownTime();
        String time = TimeFormatUtil.parseTime(countDown);
        Spanny spanny = new Spanny(getString(R.string.fabi_orderRecordDetailActivity22))
                .append(String.format("“%s”", time), new ForegroundColorSpan(color(R.color.fabiApp)))
                .append(String.format(getString(R.string.fabi_orderRecordDetailActivity23), data.getSellUsername()))
                .append(String.format(getString(R.string.fabi_orderRecordDetailActivity14), data.getRefer()), new ForegroundColorSpan(color(R.color.fabiApp)));
        tvBuyerFirst.setText(spanny);
        Spanny spannySecond = new Spanny(getString(R.string.fabi_orderRecordDetailActivity24))
                .append(getString(R.string.fabi_orderRecordDetailActivity25), new ForegroundColorSpan(color(R.color.fabiTextContent)))
                .append(getString(R.string.fabi_orderRecordDetailActivity26))
                .append(getString(R.string.fabi_orderRecordDetailActivity27));

        tvBuyerSecond.setText(spannySecond);

        timerDispo = Flowable.intervalRange(1, data.getCountDownTime(), 0, 1, TimeUnit.SECONDS)
                .map(i -> data.getCountDownTime() - i)
                .compose(RxSchedulersHelper.transformer())
                .subscribe(i -> {
                    String timeSpanny = TimeFormatUtil.parseTime(i);
                    Spanny spannyTime = new Spanny(getString(R.string.fabi_orderRecordDetailActivity22))
                            .append(String.format("“%s”", timeSpanny), new ForegroundColorSpan(color(R.color.fabiApp)))
                            .append(String.format(getString(R.string.fabi_orderRecordDetailActivity23), data.getSellUsername()))
                            .append(String.format(getString(R.string.fabi_orderRecordDetailActivity14), data.getRefer()), new ForegroundColorSpan(color(R.color.fabiApp)));
                    tvBuyerFirst.setText(spannyTime);
                });


        ClickUtil.click(btBuyerSure, () -> {//标记付款
            showPayTip();
        });
        ClickUtil.click(tvBuyerCancel, () -> {//取消订单
            showCancelTip();
        });
        refreshTimerOrder();
    }

    /**
     * 买家等待放币
     */
    private void buyerWaitCoin() {
        llBuyerWaitCoin.setVisibility(View.VISIBLE);
        Spanny append = new Spanny(getString(R.string.fabi_orderRecordDetailActivity28))
                .append(String.format(getString(R.string.fabi_orderRecordDetailActivity14), data.getRefer()), new ForegroundColorSpan(color(R.color.fabiApp)));
        tvBuyerWaitCoinFirst.setText(append);
        String time = TimeFormatUtil.parseTime(data.getCountDownTime());
        Spanny spannyTime = new Spanny(getString(R.string.fabiorderRecordDetailActivity10))
                .append(String.format("“%s”", time), new ForegroundColorSpan(color(R.color.fabiTextContent)))
                .append(getString(R.string.fabi_orderRecordDetailActivity30));
        tvBuyerWaitCoinSecond.setText(spannyTime);

        timerDispo = Flowable.intervalRange(1, data.getCountDownTime(), 0, 1, TimeUnit.SECONDS)
                .map(i -> data.getCountDownTime() - i)
                .compose(RxSchedulersHelper.transformer())
                .subscribe(i -> {
                    String timeIn = TimeFormatUtil.parseTime(i);
                    Spanny spannyTimeIn = new Spanny(getString(R.string.fabiorderRecordDetailActivity10))
                            .append(String.format("“%s”", timeIn), new ForegroundColorSpan(color(R.color.fabiTextContent)))
                            .append(getString(R.string.fabi_orderRecordDetailActivity30));
                    tvBuyerWaitCoinSecond.setText(spannyTimeIn);
                });
        refreshTimerOrder();

    }

    /**
     * 支付完成提示弹窗
     */
    private void showPayTip() {
        if (checkBoxWechat.isChecked()) {
            positionPay = 1;
        } else if (checkBoxAlipay.isChecked()) {
            positionPay = 2;
        } else if (checkBoxBank.isChecked()) {
            positionPay = 3;
        } else {
            ToastUtil.showShort(getString(R.string.fabi_orderRecordDetailActivity31));
            return;
        }
        if (payTipDialog == null) {
            payTipDialog = TipUtil.getSureTip(this, App.INSTANCE.getString(R.string.fabiorderRecordDetailActivity22), App.INSTANCE.getString(R.string.fabi_orderRecordDetailActivity33), () -> mPresenter.markPay(positionPay, orderId));
        }
        payTipDialog.show();
    }

    /**
     * 显示二维码弹窗
     */
    private void showQR(String img) {
        TipFabiUtil.showQR(this, App.INSTANCE.getString(R.string.fabi_orderRecordDetailActivity34), null, img);
    }

    /**
     * 显示撤单提示
     */
    private void showCancelTip() {
        if (tipCancleDialog == null) {
            tipCancleDialog = TipUtil.getSureTip(this, App.INSTANCE.getString(R.string.fabiorderRecordDetailActivity21), App.INSTANCE.getString(R.string.fabi_orderRecordDetailActivity36), () -> {
                mPresenter.cancelOrder(orderId);
            });

        }
        tipCancleDialog.show();
    }


    /**
     * 定时刷新订单信息
     */
    private void refreshTimerOrder() {
        if (detailDispo == null || detailDispo.isDisposed()) {
            detailDispo = Flowable.interval(0, 5, TimeUnit.SECONDS)
                    .subscribe(aLong -> {
                        mPresenter.getOrderInfo(orderNum);
                    });
        }
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timerDispo);
        DisposUtil.close(detailDispo);
        super.onDestroy();
    }

    // 支付方式单选
    public void checkBoxSelected(int position) {
        switch (position) {
            case 1:
                checkBoxAlipay.setChecked(false);
                checkBoxBank.setChecked(false);
                break;
            case 2:
                checkBoxWechat.setChecked(false);
                checkBoxBank.setChecked(false);
                break;
            case 3:
                checkBoxAlipay.setChecked(false);
                checkBoxWechat.setChecked(false);
                break;
        }
    }
}
